package com.ciban.smarthome.module.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.db.DbBaseDao;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.entity.Equip_Curtain;
import com.ciban.smarthome.module.entity.Equipment;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/10.
 */
public class CurtainDao extends DbBaseDao {
    public CurtainDao(Context context) {
        super(context, "curtain");
    }

    /*_id integer, scene_id integer default -1,status integer*/

    /**
     * 添加新的窗帘配置
     *
     * @param info
     * @return
     */
    public long addNewCurtain(Equip_Curtain info) {
        ContentValues values = new ContentValues();
        values.put("_id", info.getId());
        values.put("scene_id", info.getScene_id());
        values.put("status", info.getStatus());

        return insert(null, values);
    }


    public int upCurtain(Equip_Curtain info) {
        ContentValues values = new ContentValues();
        values.put("_id", info.getId());
        values.put("scene_id", info.getScene_id());
        values.put("status", info.getStatus());

        return update(values, "_id=?", new String[]{info.getId()});
    }

    public int delCurtain(String id) {
        return del("_id=?", new String[]{id});
    }

    /**
     * 连表获取窗帘的详细信息
     * @param area_id
     * @param type
     * @return
     */
    public ArrayList<Equip_Curtain> getCurtainList(String area_id, int type) {
        ArrayList<Equip_Curtain> list = new ArrayList<Equip_Curtain>();
        String sql = "select  e.[_id],e.[area_id],e.[icon],e.[name],e.[serial_num],e.[type],l.[scene_id],l.[status]   from equipment e,curtain l where e.[type]=? and e.[area_id]=? and l.[scene_id]=-1 and e.[_id]=l.[_id] ";
        Cursor cursor = execRawQuery(sql, new String[]{type + "", area_id});
        if (cursor != null) {
            Equip_Curtain info;
            while (cursor.moveToNext()) {
                info = new Equip_Curtain();
                info.setId(cursor.getString(0));
                info.setArea_id(cursor.getString(1));
                info.setIcon_path(cursor.getString(2));
                info.setName(cursor.getString(3));
                info.setSerial_no(cursor.getInt(4));
                info.setType(cursor.getInt(5));
                info.setScene_id(cursor.getString(6));
                info.setStatus(cursor.getInt(7));

                list.add(info);
            }
        }
        return list;
    }
}
